home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / ABox.v1.8 / CPlus Files / ABText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-23  |  12.0 KB  |  525 lines  |  [TEXT/MMCC]

  1. /*    
  2.     Copyright © 1991-1995 by TopSoft Inc.  All rights reserved.
  3.  
  4.     You may distribute this file under the terms of the TopSoft
  5.     Artistic License, accompanying this package.
  6.     
  7.     This file was developed by George (ty) Tempel in connection with TopSoft, Inc..
  8.     See the Modification History for more details.
  9.  
  10. Product
  11.     About Box
  12.  
  13. FILE
  14.     ABText.c
  15.  
  16. NAME
  17.     ABText.c, part of the ABox project source code,
  18.     responsible for handling the AboutBox Text class stuff.
  19.  
  20. DESCRIPTION
  21.     This file contains defines for the about box modules.
  22.     
  23. DEVELOPED BY
  24.     George (ty) Tempel                netromancr@aol.com
  25.     All code in this file, and its associated header file was
  26.     Created by George (ty) Tempel in connection with the TopSoft, Inc.
  27.     "FilterTop" application development, except where noted.
  28.  
  29. CARETAKER - George (ty) Tempel <netromancr@aol.com>
  30.      Please consult this person for any changes or suggestions to this file.
  31.  
  32. MODIFICATION HISTORY
  33.  
  34.     dd mmm yy    -    xxx    -    patchxx: description of patch
  35.     9 June 94    -    ty    -    Initial Version Created
  36.     20-july-94    -    ty    -    initial version released
  37.     28-july-94    -    ty    -    1.0.6--additions to support settable drag mgr usage
  38.                                     via the ABox Get/SetProperty methods
  39.     26-apr-95    -    ty    -    1.0.7--small fixes to tighten up scrolling vs drag handling
  40.     23-may-95    -    ty    -    changes for compatibility with the CodeWarrior CW6
  41.                             release and the associated Universal Headers from Apple:
  42.                             most methods that returned references now have "Ref" at
  43.                             the end of their methods names to prevent possible collisions
  44.                             with datatypes and classes of the same name (older versions
  45.                             of the compiler didn't have a problem with this).
  46. */
  47.  
  48. /*===========================================================================*/
  49.  
  50. /*======= Segmentation directives ========*/
  51.  
  52. #ifdef USE_MANUAL_SEGMENTATION
  53. #pragma segment ty
  54. #endif
  55.  
  56. /*============ Header files ==============*/
  57.     
  58. #include     "ABText.h"
  59. #include    "ABox.h"
  60.  
  61. /*=============== Globals ================*/
  62.  
  63. /*================ CODE ==================*/
  64.  
  65.  
  66. /*=============================== ABText::ABText ================================*/
  67. ABText::ABText(void)
  68. {
  69.     //visibleObject = true;
  70.     mTEh = NULL;
  71.     mScrollH = NULL;
  72.     mResType = kABtextResource;
  73. } // end ABText
  74.  
  75.  
  76.  
  77.  
  78. /*=============================== ABText::~ABText ================================*/
  79. ABText::~ABText(void)
  80. {
  81.     if (this->HasTEh())
  82.     {
  83.         ::TEDispose (this->TEh());
  84.         this->TEh() = NULL;
  85.     }
  86.     
  87.     if (this->HasScrollbar())
  88.     {
  89.         ::DisposeControl(this->Scrollbar());
  90.         this->Scrollbar() = NULL;
  91.     } 
  92.         
  93. } // end ~ABText
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /*=============================== ABText::Draw ================================*/
  100. OSErr    ABText::Draw(WindowPtr window)
  101. {
  102.     OSErr                error = noErr;
  103.     //    begin here...
  104.     
  105.     error = ABObject::Draw(window);
  106.  
  107.     if (error == noErr)
  108.         error = this->InitializeResource();
  109.  
  110.     //if (this->HasScrollbar())
  111.         this->ObjectRect().right -= kABscrollBarWidth;
  112.     
  113.     if (error == noErr)
  114.     {
  115.         Rect box;
  116.         long teHeight;
  117.         long firstLine = 0;
  118.         
  119.         box = this->ObjectRect();
  120.  
  121.         if (this->DoesntHaveTEh())
  122.         {
  123.             error = this->BuildText(&box, this->OurWindowRef());
  124.             if (this->DoesntHaveTEh())
  125.             {
  126.                 //    TENew failed, so let's just do a simple NeoTextBox here...
  127.                 //
  128.                 //    now move the right side back out because
  129.                 //    we won't use a scroll bar
  130.                 box.right -= kABscrollBarWidth;
  131.                 ::HLockHi (this->ResourceHandleRef());
  132.                 error = this->DrawTextBox (
  133.                     (unsigned char *)&(this->ResourceHandleRef())[1],    //    unsigned char *c
  134.                     **(this->ResourceHandleRef()),                    //    unsigned long textSize
  135.                     &box,                            //    Rect *displayWrapRect
  136.                     teJustCenter,                     //    short alignmentConstants
  137.                     0,                                //    lineHeightCode (0 = default)
  138.                     NULL,                            //    short *endY baseline
  139.                     NULL);                            //    short *lineHeight
  140.                 ::HUnlock (this->ResourceHandleRef());
  141.                 
  142.                 this->ObjectRect().right += kABscrollBarWidth;
  143.                 return error;
  144.             } // end if else block
  145.         
  146.         } else {
  147.         
  148.             ::TEUpdate (&box, this->TEh());
  149.         } // end if else block
  150.         
  151.         //    the scroll bar _might_ be needed, so determine just
  152.         //    how tall the text is on the screen, and how tall could
  153.         //    it be (potential height, if all text were being displayed)
  154.         //
  155.         ::HLockHi (this->ResourceHandleRef());
  156.         teHeight = ::TEGetHeight ((*(this->TEh()))->nLines, firstLine, this->TEh());
  157.         ::HUnlock (this->ResourceHandleRef());
  158.     
  159.         if (teHeight >= (box.bottom - box.top)) 
  160.         {
  161.             if (this->DoesntHaveScrollbar())
  162.             {
  163.                 //    need the scroll bar...
  164.                 Rect    scrollbarRect;
  165.                 
  166.                 scrollbarRect.top = box.top - 1;
  167.                 scrollbarRect.bottom = box.bottom + 1;
  168.                 scrollbarRect.left = box.right + 1;
  169.                 scrollbarRect.right = scrollbarRect.left + kABscrollBarWidth;
  170.                 
  171.                 this->Scrollbar() = ::NewControl (this->GetWindow(),        //    the window
  172.                                                     &scrollbarRect,            //    the rect
  173.                                                     "\pVScroll",            //    the title
  174.                                                     true,                    //    visible
  175.                                                     1,                        //    initial value
  176.                                                     1,                        //    minimum value
  177.                                                     teHeight,                //    maximum value
  178.                                                     scrollBarProc,            //    control is a scrollbar
  179.                                                     (long)(this->TEh()));    //    the reference constant
  180.                                                     
  181.                 error = ::ResError();
  182.  
  183.             }    //    end if (scrollH) block
  184.             
  185.             if (this->HasScrollbar())
  186.             {
  187.                 //    make it visible
  188.                 ::ShowControl (this->Scrollbar());
  189.                 ::HiliteControl (this->Scrollbar(), kActivateControl);
  190.                 
  191.                 this->FixScrollBar (this->Scrollbar());
  192.             } // end if else block...
  193.             
  194.         }    //    end if else block...
  195.     
  196.     }
  197.  
  198.     this->ObjectRect().right += kABscrollBarWidth;
  199.     
  200.     return error;
  201.     
  202. } // end Draw
  203.  
  204.  
  205.  
  206.  
  207. /*=============================== ABText::BuildText ================================*/
  208. OSErr    ABText::BuildText(Rect* area, WindowPtr /*window*/)
  209. {
  210.     OSErr                error = noErr;
  211.     
  212.     StScrpHandle        styleHandle = NULL;
  213.     
  214.     Rect                box;
  215.     
  216.     long                textLen = 0;
  217.     
  218.     //    begin here...
  219.     
  220.     if (area)
  221.         box = *area;
  222.     else
  223.         box = this->ObjectRect();
  224.     
  225.     //    see if we can find out a little more on the text resource...if it has
  226.     //    a corresponding 'styl' resource.
  227.     
  228.     //    we have the information, so now try to load a style 'styl' resource
  229.     //    and use it for the text.
  230.     styleHandle = (StScrpHandle)::Get1Resource (kABStyleResource, this->ResID());
  231.     error = ::ResError();
  232.     if (error) 
  233.     {
  234.         //    something's amiss...
  235.         this->TEh() = ::TENew (&box, &box);
  236.         styleHandle = NULL;
  237.         error = noErr;
  238.     } else {
  239.         //    nope, no problems, we have a TEXT and a styl resource,
  240.         //    so create a stylized text record
  241.         this->TEh() = ::TEStylNew (&box, &box);
  242.     }    //    end if block...
  243.  
  244.     if (this->DoesntHaveTEh())
  245.     {
  246.         error = paramErr;
  247.     } else {
  248.         
  249.         //    insert the text into the TEHandle
  250.         //
  251.         textLen = ::GetHandleSize (this->ResourceHandleRef());
  252.         ::HLockHi (this->ResourceHandleRef());
  253.         if (styleHandle) 
  254.         {
  255.             TEActivate (this->TEh());
  256.             TEStylInsert(*(this->ResourceHandleRef()), textLen, styleHandle, this->TEh());
  257.             TEDeactivate (this->TEh());
  258.         
  259.         } else {
  260.             TEInsert(*(this->ResourceHandleRef()), textLen, this->TEh());
  261.         }    // end if (styleHandle) block
  262.         ::HUnlock(this->ResourceHandleRef());
  263.         
  264.         //    release the style resource that shadows the text
  265.         if (styleHandle) 
  266.         {
  267.             ::ReleaseResource((Handle)styleHandle);
  268.         }    //    end if block
  269.             
  270.     }
  271.     
  272.     return error;
  273. } // end BuildText
  274.  
  275.  
  276.  
  277.  
  278.  
  279. /*=============================== ABText::Update ================================*/
  280. OSErr    ABText::Update(WindowPtr window)
  281. {
  282.  
  283.     OSErr    error = noErr;
  284.     
  285.     //    begin here...
  286.     //
  287.     //    Draw a bezel around the field _including_ the scroll bar
  288.     //
  289.     //error = ABObject::Update(window);
  290.     this->OurWindowRef() = window;
  291.     error = this->DrawFrame();
  292.     if (this->HasTEh() && (error == noErr))
  293.     {
  294.         Rect box = this->ObjectRect();
  295.         ::TEUpdate (&box, this->TEh());
  296.     } // end if block
  297.     
  298.     return error;
  299.     
  300. } // end Update
  301.  
  302.  
  303.  
  304.  
  305.  
  306. /*=============================== ABText::Event ================================*/
  307. Boolean    ABText::Event(EventRecord *event)
  308. {
  309.     Boolean        handled = false;
  310.     Point        where;
  311.     OSErr        error = noErr;
  312.     Rect        scrollBarRect;
  313.     Rect        box = this->ObjectRect();
  314.     
  315.     //    begin here...
  316.     //
  317.     if (!event || this->DoesntHaveWindow())
  318.         return false;
  319.         
  320.     switch (event->what) 
  321.     {
  322.         case    mouseDown:
  323.             //    1.0.6 ty--check to see if we're allowed to use the DragMgr
  324.             //
  325.             ABox *theABox = (ABox *)::GetWRefCon (this->OurWindowRef());
  326.             Boolean    useDrag = false;
  327.             
  328.             if (theABox)
  329.                 error = theABox->GetProperty(kABoxUseDragMgr, &useDrag, NULL);
  330.                 
  331.             where = event->where;
  332.             ::GlobalToLocal(&where);
  333.             if (::FrontWindow() != this->OurWindowRef())
  334.                 break;
  335.                 
  336.             if (this->HasScrollbar()) 
  337.             {
  338.                 scrollBarRect = (*(this->Scrollbar()))->contrlRect;
  339.                 if (::PtInRect(where, &scrollBarRect))
  340.                 {
  341.                     this->ScrollBarClick( this->Scrollbar(), where );
  342.                     this->FixScrollBar (this->Scrollbar());
  343.                     handled = true;
  344.                 } // end if block
  345.             }
  346.             
  347.             if (::PtInRect (where, &box) && ABUEnvDragMgr::IsPresent() && useDrag && !handled) {
  348.                 //    DragMgr stuff here!!!
  349.                 
  350.                 if (::WaitMouseMoved(event->where)) 
  351.                 {
  352.                     //    the user is trying to Drag
  353.                     //    from the field to someplace
  354.                     this->DragMgrEventRef() = event;
  355.                     error = this->Begin();
  356.                     error = this->End();
  357.                 } // end if block
  358.             } // end if block
  359.             break;
  360.         default:
  361.             break;
  362.     } // end switch block
  363.     
  364.     return handled;
  365. } // end Event
  366.  
  367.  
  368.  
  369.  
  370. /*=============================== ABText::Stop ================================*/
  371. OSErr    ABText::Stop(void)
  372. {
  373.     OSErr    error = noErr;
  374.     
  375.     //    begin here...
  376.     //
  377.     if (this->HasScrollbar())
  378.     {
  379.         ::DisposeControl (this->Scrollbar());
  380.         this->Scrollbar() = NULL;
  381.     } // end if block
  382.     
  383.     error = kABTextSuperProperties::Stop();
  384.     return error;
  385.     
  386. } // end Stop
  387.  
  388.  
  389.  
  390.  
  391.  
  392. /*=============================== ABText::Initialize ================================*/
  393. OSErr    ABText::Initialize(void)
  394. {
  395.     OSErr                error = noErr;
  396.     Ptr                    ptr = NULL;
  397.     Size                ptrSize = 0;
  398.     StScrpHandle        theStyle;
  399.     
  400.     //    OVERRIDE of the ABUEnvDragMgr method!
  401.     if (this->IsDragInitialized())
  402.         return noErr;
  403.     
  404.     this->DragRectRef() = this->ObjectRect();
  405.     error = ABUEnvDragMgr::Initialize();
  406.     if (error)
  407.         return error;
  408.         
  409.     error = ::SetDragSendProc (this->DragRef(),
  410.                             this->DragUPPRef(),
  411.                             this);
  412.     if (error)
  413.         return error;
  414.     
  415.     //    now create the Send information
  416.     ::HLock (this->ResourceHandleRef());
  417.     ptr = (Ptr)*(this->ResourceHandleRef());
  418.     ptrSize = ::GetHandleSize(this->ResourceHandleRef());
  419.     error = ::AddDragItemFlavor(this->DragRef(),
  420.                                 this->ItemRef(),
  421.                                 kABtextResource,
  422.                                 ptr,
  423.                                 ptrSize,
  424.                                 kABdefaultFlavorFlags);
  425.     ::HUnlock (this->ResourceHandleRef());
  426.     
  427.     //    now add the 2nd flavor, the style stuff
  428.     theStyle = ::GetStylScrap(this->TEh());
  429.     ::HLock((Handle) theStyle);
  430.     ptr = (Ptr)*theStyle;
  431.     ptrSize = ::GetHandleSize((Handle)theStyle);
  432.     error = ::AddDragItemFlavor(this->DragRef(),
  433.                                 this->ItemRef(), 
  434.                                 kABstyleResource, 
  435.                                 ptr, 
  436.                                 ptrSize,
  437.                                 kABdefaultFlavorFlags);
  438.     ::HUnlock((Handle) theStyle);
  439.     ::DisposeHandle((Handle) theStyle);
  440.  
  441.  
  442.     this->DragInitializedRef() = true;    
  443.     
  444.     return error;
  445.  
  446. }    // end Initialize
  447.  
  448.  
  449.  
  450.  
  451.  
  452. /*=============================== ABText::Begin ================================*/
  453. OSErr    ABText::Begin(void)
  454. {
  455.     OSErr    error;
  456.     
  457.     //    OVERRIDE of the ABUEnvDragMgr method! but be certain to call
  458.     //    the ABUEnvDragMgr method when finished here!!!!
  459.     
  460.     error = this->Initialize();
  461.     if (error)
  462.         return error;
  463.         
  464.     error = ABUEnvDragMgr::Begin();
  465.     
  466.     return error;
  467.  
  468. }    // end Begin
  469.  
  470.  
  471.  
  472.  
  473.  
  474. /*=============================== ABText::LocalSendProc ================================*/
  475. //
  476. OSErr    ABText::LocalSendProc(FlavorType theType,
  477.                                     void* /*dragSendRefCon*/,
  478.                                     ItemReference theItemRef,
  479.                                     DragReference theDragRef)
  480. {
  481.     OSErr                error = noErr;
  482.     Ptr                    ptr = NULL;
  483.     Size                ptrSize = 0;
  484.     StScrpHandle        theStyle;
  485.  
  486.     //    begin here...
  487.     
  488.     if (theType == kABstyleResource) 
  489.     {
  490.  
  491.         theStyle = ::GetStylScrap(this->TEh());
  492.  
  493.         //
  494.         //    Call SetDragItemFlavorData to provide the requested data.
  495.         //
  496.  
  497.         ::HLock((Handle) theStyle);
  498.         ptr = (Ptr)*theStyle;
  499.         ptrSize = ::GetHandleSize((Handle)theStyle);
  500.         error = ::SetDragItemFlavorData(theDragRef, 
  501.                                 theItemRef, 
  502.                                 kABstyleResource, 
  503.                                 ptr,
  504.                                 ptrSize,
  505.                                 kABdefaultFlavorFlags);
  506.         ::HUnlock((Handle) theStyle);
  507.         ::DisposeHandle((Handle) theStyle);
  508.  
  509.     } else {
  510.  
  511.         return badDragFlavorErr;
  512.  
  513.     }
  514.  
  515.     return error;
  516.  
  517. } // end LocalSendProc()
  518.  
  519.  
  520.  
  521.  
  522.  
  523. //    end of file
  524.  
  525.